package com.tocean.app.controller.framework;

import com.tocean.app.controller.framework.request.QuestionSaveRequest;
import com.tocean.app.controller.framework.search.QuestionSearchDto;
import com.tocean.app.dto.request.BasePageRequest;
import com.tocean.app.dto.request.BaseRequest;
import com.tocean.app.dto.response.BasePageResponse;
import com.tocean.app.dto.response.BaseResponse;
import com.tocean.app.entity.framework.Question;

import com.tocean.app.service.framework.QuestionService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

@Api(tags = "题目")
@Slf4j
@Validated
@CrossOrigin
@RestController("FrameworkQuestionController")
@RequestMapping("/framework/question")
public class QuestionController {

    @Autowired
    private QuestionService service;

    /**
     * 功能描述：查询[题目]详情
     *
     * @param id 标识
     * @return
     */
    @CrossOrigin
    @ApiOperation(value = "查询[题目]详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "标识", dataType = "String", paramType = "path", required = true, defaultValue = "")
    })
    @GetMapping("/{id:\\d+}")
    public ResponseEntity<BaseResponse<?>> findById(@NotBlank @PathVariable("id") String id) {
        return ResponseEntity.ok(BaseResponse.ok(service.findById(id)));
    }

    /**
     * 功能描述：查询[题目]列表
     *
     * @return
     */
    @CrossOrigin
    @ApiOperation(value = "查询[题目]列表")
    @PostMapping ("/page")
    public ResponseEntity<BaseResponse<BasePageResponse<Question>>> page(@Valid @RequestBody BasePageRequest<QuestionSearchDto> request) {
        return ResponseEntity.ok(BaseResponse.ok(service.page(request.getData(), request.getPage(), request.getLimit())));
    }

    /**
     * 功能描述：保存[题目]
     *
     * @param request
     * @return
     */
    @CrossOrigin
    @ApiOperation(value = "保存[题目]")
    @PostMapping("")
    public ResponseEntity<BaseResponse<?>> save(@Valid @RequestBody BaseRequest<QuestionSaveRequest> request) {
        service.save(request.getData());
        return ResponseEntity.ok(BaseResponse.ok());
    }

    /**
     * 功能描述：更新[题目]
     *
     * @param request
     * @return
     */
    @CrossOrigin
    @ApiOperation(value = "更新[题目]")
    @PutMapping("/{id:\\d+}")
    public ResponseEntity<BaseResponse<?>> update(@NotBlank @PathVariable("id") String id,@Valid @RequestBody BaseRequest<QuestionSaveRequest> request) {
        service.update(id,request.getData());
        return ResponseEntity.ok(BaseResponse.ok());
    }


    /**
     * 功能描述：替换[题目]删除状态
     *
     * @param id
     * @return
     */
    @CrossOrigin
    @ApiOperation(value = "替换[题目]删除状态")
    @DeleteMapping("/{id:\\d+}")
    public ResponseEntity<BaseResponse<?>> del(@Valid @ApiParam("标识") @PathVariable String id) {
        service.handleIsDel(id);
        return ResponseEntity.ok(BaseResponse.ok());
    }

}
